package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.SchoolDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;

public class GetAllSchool extends HttpServlet {
	//http://pingkgs3pc:8080/zuiaixue/getallschool
	//http://zuiaixue.jd-app.com/getallschool
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Connection conn = DBUtil.getConnection();
		if (conn == null) {
			O.printDBerror(response);
			return;
		}
		
		DAOFactory dao = new DAOFactory(conn, request, response);
		SchoolDao sdao = dao.getSchoolDao();
		List<BaseEntity> list = sdao.getAllSchool();
		if (list.size()==0) {
			O.printFail(response, 1, "暂无学校数据!");
			return;
		}
		
		List<Map<String, Object>> newlist = new ArrayList<Map<String,Object>>();
		for (BaseEntity be:list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("schoolid", be.getId());
			map.put("schoolname", be.getName());
			
			newlist.add(map);
		}
		O.printSuccess(response, newlist);
	}

}
